<template>
	<div>
		<span v-if="showoerr" style="font-size:50px">{{wrongmsg}}</span>
		<Home v-if="showhome" />
	</div>
</template>

<script>
	import {
		commonPost
	} from "@api/data";
	import Home from "./Home";
	import {
		CallMedIns
	} from "@api/tools/MedInsTrade.js";
	import moment from "moment";
	import {
		SystemSet
	} from '@api/tools/Hardware.js'
	// import ZzfkUrl from "@images/zhuce.png";
	// import MzghUrl from "@images/clock.png";
	// import MzcxUrl from "@images/list.png";
	// import MzjsUrl from "@images/pay.png";
	// import MzczUrl from "@images/withdrawal.png";
	// import OthUrl from "@images/more.png";
	// import PriUrl from "@images/printer.png";
	// import ZyczUrl from "@images/ihpay.png";
	// import QdcxUrl from "@images/qdcx.png";
	export default {
		name: "homepage",
		data() {
			return {
				YB: new CallMedIns(),
				System: new SystemSet(),
				showoerr: false,
				showhome: false,
				isVerticalScreen: false,
				wrongmsg: "",
				terminfo: [],
				seceninfo: [],
				funcinfo: []
			};
		},
		methods: {
			//可以获取到终端信息  没用
			getTerminalInfo() {
				this.$store.dispatch("terminfo/updateseceninfo", "");
				this.$store.dispatch("terminfo/updatefuncinfo", "");
				this.$store.dispatch("terminfo/updateterminfo", "");
				this.$store.dispatch("terminfo/updatehospName", "");
				let data = {};
				data.url = "/api/sst/GetTerminalInfo";
				data.payload = {};
				commonPost(data)
					.then(res => {
						this.terminfo = res.data;
						this.getTerminalScen(this.terminfo.sceneid, this.terminfo.orgid);
						this.$store.dispatch("terminfo/updateterminfo", res.data);
						sessionStorage.setItem("terminfo", JSON.stringify(res.data));
					})
					.catch(err => {
						this.showoerr = true;
						this.wrongmsg = "服务异常，请联系管理员处理！";
					});
			},
			//对应的场景信息，都有哪些配置，做配置项，home动态加载
			getTerminalScen(sceneid, orgid) {
				let data = {};
				data.url = "/api/sst/GetSstSysConf";
				data.payload = {
					sceneid: sceneid,
					orgid: orgid
				};
				commonPost(data).then(res => {
					let functionInfo = res.data.functionInfo;
					// let resolution = res.data.senceInfo[0].resolution.split("*");
					// this.isVerticalScreen = parseInt(resolution[0]) < parseInt(resolution[1]) ? true:false;
					// console.log("this.isVerticalScreen",this.isVerticalScreen);
					this.$store.dispatch("terminfo/updatehospName", res.data.hospName);
					this.$store.dispatch("terminfo/updatehospLogo", res.data.hospLogo);
					this.$store.dispatch("terminfo/updateorginfo", res.data.orginfo);
					this.$store.dispatch("terminfo/updateseceninfo", res.data.senceInfo[0]);
					// this.$store.dispatch("terminfo/updatefuncinfo",this.funcinfo);
					this.$store.dispatch("terminfo/updatefuncinfo", functionInfo);
					this.ybinit();
					this.showhome = true;
					this.showoerr = false;
				});
			},
			async ybsign() {
				var CmiSign = '';
				var CmiBusinessInfo = '';
				var datetime = moment().format("YYYYMMDDHHmmSS");
				let orginfo = this.$store.state.terminfo.orginfo;
				let termId = this.$store.state.terminfo.terminfo.termId;
				var operno = orginfo.cmiid + termId.substr(2, 1) + termId.substr(4, 2);
				this.$store.dispatch("terminfo/updateyboperno", operno);
				// eslint-disable-next-line no-console
				console.log(
					"res=" + datetime + "；cmiid=" + orginfo.cmiid + ";operno=" + this.$store.state.terminfo
					.yboperno
				);
				if (orginfo.cmiid === '104') {
					CmiSign = "9100^" + orginfo.cmiid + "^" + operno + "^^" + datetime + orginfo.cmiid +
					"001^0000^^1^";
					// eslint-disable-next-line no-console
					console.log("开始签到" + CmiSign);
					let temp = await this.YB.Sign(1, CmiSign);
					let temp2 = temp.split("^")[2];
					CmiBusinessInfo = temp2.split("|")[0];
					// eslint-disable-next-line no-console
					console.log("CmiBusinessInfo=" + CmiBusinessInfo);

				} else {
					CmiSign = "9100^" + orginfo.cmiid + "^" + operno + "^^" + datetime + orginfo.cmiid + "001^^^";
					// eslint-disable-next-line no-console
					console.log("开始签到" + CmiSign);
					let temp = await this.YB.Sign(1, CmiSign);
					CmiBusinessInfo = temp.split("^")[1];
					// eslint-disable-next-line no-console
					console.log("CmiBusinessInfo=" + CmiBusinessInfo);
				}


				this.$store.dispatch("terminfo/updatemibusinessinfo", CmiBusinessInfo);

				// var PmiSign='9100^'+orginfo.cmiid+'^'+termId+'^^'+datetime+orginfo.cmiid+'002^229900^^1^';
				// let PMIBusinessInfo= this.YB.MISign(2,'省医保签到报文');
			},
			async ybinit() {
				this.YB.Init(1).then(() => this.ybsign()).catch(e => {
					console.error("医保初始化失败");
				});
			},
			systemInit() { //系统初始化请求系列配置
				this.$store.dispatch("terminfo/updateterminfo", ""); //终端信息
				this.$store.dispatch("terminfo/updatedeviceConfs", ""); //终端硬件信息
				sessionStorage.clear(); //终端硬件信息
				this.$store.dispatch("terminfo/updatehospName", ""); //医院名称
				this.$store.dispatch("terminfo/updatehospLogo", ""); //医院logo
				this.$store.dispatch("terminfo/updateorginfo", ""); //机构信息
				this.$store.dispatch("terminfo/updateseceninfo", ""); //场景信息
				this.$store.dispatch("terminfo/updatefuncinfo", ""); //场景功能信息
				this.$store.dispatch("terminfo/updateloginConfs", ""); //读卡配置信息
				let data = {};
				data.url = "/api/sst/conf/GetSstSysConf";
				data.payload = {};
				commonPost(data).then(res => {
					let sstTerminal = res.data.sstTerminal
					let sstDevs = res.data.sstDevstatuses;
					for (let item of sstDevs) {
						if (item.devtype == "1") {
							sstTerminal["receiptPrinterModel"] = item.devmodel; //这里是为了后边取值改动不变
							console.log("终端信息=" + JSON.stringify(sstTerminal));
							break;
						}
					}
					this.$store.dispatch("terminfo/updateterminfo", sstTerminal); //终端信息
					this.$store.dispatch("terminfo/updatedeviceConfs", res.data.sstDevstatuses); //终端硬件信息
					sessionStorage.setItem("terminfo", JSON.stringify(res.data.sstDevstatuses)); //终端硬件信息
					console.log("sysDateTime=" + res.data.sysDateTime);
					this.$store.dispatch("terminfo/updatehospName", res.data.hospName); //医院名称
					this.$store.dispatch("terminfo/updatehospLogo", res.data.hospLogo); //医院logo
					this.$store.dispatch("terminfo/updateorginfo", res.data.orginfo); //机构信息
					this.$store.dispatch("terminfo/updateseceninfo", res.data.sstScene); //场景信息
					this.$store.dispatch("terminfo/updatefuncinfo", res.data.sstSceneFuncconfs); //场景功能信息
					// this.$store.dispatch("terminfo/updateloginConfs", res.data.loginConfs);//读卡配置信息
					console.log("yibao=" + res.data.orginfo.cmiid);
					if (res.data.orginfo.cmiid !== "" || res.data.orginfo.cmiid != undefined) { //医保编码不为空则进行医保初始化
						this.ybinit();
					}
					this.System.SysSetTime(res.data.sysDateTime); //设置本机时间与服务器时间一致。

					this.showhome = true;
					this.showoerr = false;
				});
			},
		},
		mounted() {
			var sceneid = this.$store.state.terminfo.seceninfo.sceneid;
			if (sceneid !== undefined) { //不为空则进行刷新更新，为空则是第一次启动加载
				location.reload();
			} else {
				this.systemInit();
			}

		},
		beforeCreate() {

		},
		computed: {
			// 计算属性
			statusData() {
				return this.showoerr;
			}
		},
		watch: {
			statusData: function(showoerr) {
				console.log("sss");
				// 当返回的新值为创建中的时候,保持3秒轮询
				if (showoerr) {
					this.timer = setInterval(() => {
						setTimeout(this.systemInit, 100);
					}, 15000);
				}
				// 当请求正常时
				if (!showoerr) {
					clearInterval(this.timer);
				}
			}
		},
		components: {
			Home
		}
	};
</script>

<style scoped>
</style>
